package cn.leida.ruoyi.common.core.page;

import cn.leida.ruoyi.common.utils.ServletUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * 表格数据处理
 *
 * @author ruoyi
 */
public class TableSupport {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";
    /**
     * 当前记录起始索引
     */
    public static final String CURRENT_PAGE = "currentPage";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain() {
        PageDomain pageDomain = new PageDomain();

        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        // 从currentPage中解析
        Integer currentPage = ServletUtils.getParameterToInt(CURRENT_PAGE);
        if (currentPage != null && currentPage > 0) {
            pageDomain.setPageNum(currentPage);
        }
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
        return pageDomain;
    }

    public static PageDomain buildPageRequest() {
        return getPageDomain();
    }

    public static Pageable pageSortDesc(String defColumn) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize(), pageDomain.sortDesc(defColumn));
    }

    public static Pageable pageSortAsc(String defColumn) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize(), Sort.by(Sort.Order.asc(defColumn)));
    }

    public static Pageable page() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize());
    }

    /**
     * 根据页面参数进行选择
     */
    public static Pageable pageSort(String defColumn) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize(), pageDomain.sort(defColumn));
    }


    /**
     * 默认分页
     */
    public static Pageable defPage(String defColumn) {
        PageDomain pageDomain = new PageDomain();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize(), pageDomain.sort(defColumn));
    }

    public static Pageable defPage() {
        PageDomain pageDomain = new PageDomain();
        return PageRequest.of(pageDomain.pageNo(), pageDomain.pageSize());
    }

    /**
     * 默认排序: 倒序
     *
     * @param sortColumn 排序字段
     */
    public static Sort sortDesc(String sortColumn) {
        return Sort.by(Sort.Order.desc(sortColumn));
    }

    /**
     * 默认排序: 顺序
     *
     * @param sortColumn 排序字段
     */
    public static Sort sortAsc(String sortColumn) {
        return Sort.by(Sort.Order.asc(sortColumn));
    }

}
